package com.example.takeoff.controller;

import com.example.takeoff.common.constants;
import com.example.takeoff.controller.dto.userDto;
import com.example.takeoff.entity.DDL;
import com.example.takeoff.common.Result;
import com.example.takeoff.entity.user;
import com.example.takeoff.service.impl.CheckServiceImpl;
import com.example.takeoff.service.impl.DDLServiceImpl;
import com.example.takeoff.service.impl.userServiceImpl;
import com.example.takeoff.utils.DateChange;
import com.example.takeoff.utils.tokenUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/DDLReminder")
public class DDLController
{
    @Autowired
    private DDLServiceImpl ddlService;
    @Autowired
    private userServiceImpl userService;
    @Autowired
    private CheckServiceImpl checkService;


    @GetMapping("/{userid}")
    public Result getByUserid(@PathVariable String userid)
    {
        if (ddlService.getByUserid(userid).isEmpty())
            return Result.error(constants.code_1100, "用户不存在");
        return Result.success(ddlService.getByUserid(userid));
    }

    @GetMapping("/classifiedDDL/{userid}")
    public Result getClassifiedDDL(@PathVariable String userid)
    {
        return Result.success(ddlService.getClassifiedDDL(userid));
    }

    @PostMapping("/Register")
    public Result Register(@RequestBody userDto userDto)
    {
        Result result = checkService.checkCode(userDto.getPhoneNum(), userDto.getCode());
        if (!result.getCode().equals(constants.code_200))
            return result;
        String username = userDto.getUsername();
        String password = userDto.getPassword();
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password))
            return Result.error(constants.code_400, "参数不足");
        user register = userService.Register(userDto);
        return Result.success(register);
    }

    @PostMapping("/Login")
    public Result Login(@RequestBody userDto userDto)
    {
        String username = userDto.getUsername();
        String password = userDto.getPassword();
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password))
            return Result.error(constants.code_400, "参数不足");
        userDto dto = userService.Login(userDto);
        return Result.success(dto);
    }

    @PostMapping
    public Result save(@RequestBody DDL ddl)
    {
        boolean flag = ddlService.save(ddl);
        if (flag)
            return Result.success("添加成功");
        else
            return Result.error(constants.code_300, "添加失败");
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable int id)
    {
        return ddlService.delete(id);
    }

    @PutMapping("/classify/{id}")
    public Result classify(@PathVariable int id)
    {
        return ddlService.classify(id);
    }

    @PutMapping("{id}")
    public Result recover(@PathVariable int id)
    {
        return ddlService.recover(id);
    }

    @PostMapping("/update")
    public Result update(@RequestBody DDL ddl)
    {
        boolean flag = ddlService.modify(ddl);
        if (flag)
            return Result.success(constants.code_200, "修改成功");
        else
            return Result.error(constants.code_300, "修改失败");
    }

    @GetMapping("guidang/{userid}")
    public Result Gui(@PathVariable String userid)
    {
        return Result.success(ddlService.getClassifiedDDL(userid));
    }
}
